package edu.ctbu.ctbujsj.webapi;

import edu.ctbu.ctbujsj.domain.Student;
import edu.ctbu.ctbujsj.exception.REnum;
import edu.ctbu.ctbujsj.helper.RUtil;
import edu.ctbu.ctbujsj.service.StudentService;
import edu.ctbu.ctbujsj.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class LoginRestController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/webapi/login/student")
    public R student(HttpServletRequest request, String name, String password) throws Exception{

        Student student=studentService.validUserNameAndPassword(name,password);
        if(student!=null){
            HttpSession session=request.getSession();

            session.setAttribute("userid",student.getId());

            session.setAttribute("user",student);


            return RUtil.success();
        }

        return RUtil.error(REnum.COMMON_ERR);

    }
}
